﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MetroCashLib;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MetroCash
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            Loaded += MainPage_Loaded;
        }

        async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //var dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "StartUp.metrocash");
            //using (var db = new SQLite.SQLiteConnection(dbPath))
            //{
            //    db.CreateTable<MetroCashLib.DataInfo.AccountType>();
            //    db.CreateTable<MetroCashLib.DataInfo.Currency>();
            //    db.CreateTable<MetroCashLib.DataInfo.Account>();
            //    db.CreateTable<MetroCashLib.DataInfo.Transaction>();
            //    db.Close();
            //}

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        async protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            bool noProfile = false;
            bool profileMissed = false;
            Windows.Storage.ApplicationDataContainer roamingSetting = Windows.Storage.ApplicationData.Current.RoamingSettings;
            if (roamingSetting.Values[SettingStrings.ROAMING_CURRENT_PROFILE] == null)
            {
                noProfile = true;
            }
            else
            {
                try
                {
                    var profileFolder = await Windows.Storage.ApplicationData.Current.RoamingFolder.GetFolderAsync(SettingStrings.PROFILE_FOLDER_NAME);
                    var files = await profileFolder.GetFilesAsync();
                    if (files.Count > 0)
                    {
                        var profileFile = await StorageFile.GetFileFromPathAsync(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, roamingSetting.Values[SettingStrings.ROAMING_CURRENT_PROFILE].ToString())));
                    }
                    else
                    {
                        noProfile = true;
                    }
                }
                catch
                {
                    profileMissed = true;
                }
            }

            if (this.Frame != null)
            {
                if (noProfile)
                {
                    // If there is no Profile, navigate to the "Create new profile page"
                    this.Frame.Navigate(typeof(PCreateProfile));
                }
                else if (profileMissed)
                {

                }
                else
                {
                    this.Frame.Navigate(typeof(PProfileAccounts), roamingSetting.Values[SettingStrings.ROAMING_CURRENT_PROFILE].ToString());
                }
            }
        }
    }
}
